<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-20
 * Func   彩票投注
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('lottery_order_list');
$ptable = Table('lottery_period');
$ptable2 = Table('lottery_play');
$ctable = Table('lottery');
$utable = Table('user');

if($a == 'del')
{
	$id = intval($_GET['id']);
	$flag = $_GET['flag'] ? 1 : 0;
	
    //$id && $db->exec('DELETE FROM '.$table.' WHERE id = '.$id);
	
	ShowMsg('操作成功',-1);
}
else
{
	//	彩票种类
	$lottery = $db->getall('SELECT id,title,credit FROM '.Table('lottery').' WHERE status = 1 ORDER BY id','id');
	
	//	状态
	$statuss = ['待开奖','已派奖','已撤单','已删除',88=>'已中奖',99=>'开奖中'];

    $data  = array();
    $where = ' FROM '.$table.' t LEFT JOIN '.$ptable.' p ON t.iid = p.id LEFT JOIN '.$utable.' u ON t.uid = u.id LEFT JOIN '.Table('user_status').' s ON t.uid = s.uid WHERE s.upuid = '.$_uid;
	
	$cid = intval($_GET['cid']);
	$cid && $where .= ' AND t.cid = '.$cid;
	
	$iid = intval($_GET['iid']);
	$iid && $where .= ' AND t.iid = '.$iid;
	
	$uid = intval($_GET['uid']);
	$uid && $where .= ' AND t.uid = '.$uid;
	
	$pid = intval($_GET['pid']);
	$pid && $where .= ' AND t.pid = '.$pid;
	
	//	时间
	$dt1 = NoBadStr($_GET['dt1']);
	$dt1 && $where .= ' AND t.dateline >= '.strtotime($dt1);
	$dt2 = NoBadStr($_GET['dt2']);
	$dt2 && $where .= ' AND t.dateline <= '.strtotime($dt2);
	
	$status = intval($_GET['status']);
	$status && $where .= ' AND t.status = '.($status > 0 ? ($status == 88 ? $status.' AND t.prize > 0' : $status) : 0);
	
	$title = NoBadStr($_GET['title']);
	$title && $where .= ' AND (p.period LIKE \'%'.$title.'%\' OR u.username LIKE \'%'.$title.'%\')';

    $tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
    if($tmp['p'])
	{
        $page = max(1,intval($_GET['page']));
        $size = 20;
        $limit    = ' LIMIT '.($page - 1) * $size.','.$size;

        $orderby = ' ORDER BY t.id DESC ';
        $url = '?m='.$m.'&c='.$c.'&a='.$a.'&uid='.$uid.'&cid='.$cid.'&iid='.$iid.'&pid='.$pid.'&status='.$status.'&title='.$title.'&dt1='.$dt1.'&dt2='.$dt2;
		
        $data = $db->getall('SELECT t.*,p.period,p.rednum AS result,u.username '.$where.$orderby.$limit);
		foreach($data as $key => $val)
		{
			$t2 = $db->getrow('SELECT pid,name FROM '.$ptable2.' WHERE cid = '.$val['cid'].' AND playid = '.$val['pid']);
			$t3 = $db->getrow('SELECT name FROM '.$ptable2.' WHERE id = '.$t2['pid']);
			
			$data[$key]['pname'] = $t3['name'].' '.$t2['name'];
		}
        
		$multi = Multi($url,$page,$tmp['p'],$size);
    }

    $tpl->display($m.'/order');
}